package com.lib.framework.helper

import android.content.Context
import android.util.Base64
import java.io.*


/**
 * SharedPreferences 辅助类
 *
 * @author keanbin
 */
open class SharedPreferencesHelper(context: Context, fileName: String) {

    companion object {

        /**
         * 保存对象，一个对象一个文件
         */
        fun <T : Serializable> saveObj(context: Context, preferenceName: String, key: String, obj: T): Boolean {
            val sharedPreferences = context.getSharedPreferences(preferenceName, Context.MODE_PRIVATE)
            val editor = sharedPreferences.edit()
            val out = ByteArrayOutputStream()
            try {
                val oos = ObjectOutputStream(out)
                oos.writeObject(obj)//把对象写到流里
                val temp = String(Base64.encode(out.toByteArray(), Base64.DEFAULT))
                editor.putString(key, temp)
                editor.commit()
                return true
            } catch (e: Exception) {
                e.printStackTrace()
            }

            return false
        }

        /**
         * 读取对象，一个对象一个文件
         */
        fun <T : Serializable> getObj(context: Context, preferenceName: String, key: String): T? {
            val sharedPreferences = context.getSharedPreferences(preferenceName, Context.MODE_PRIVATE)
            val temp = sharedPreferences.getString(key, "") ?: return null
            val inputStream = ByteArrayInputStream(Base64.decode(temp.toByteArray(), Base64.DEFAULT))
            try {
                val ois = ObjectInputStream(inputStream)
                return ois.readObject() as T
            } catch (e: Exception) {
                e.printStackTrace()
            }

            return null
        }
    }

    private var mContext = context

    private var mSharedPreferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE)

    private var mEditor = mSharedPreferences.edit()

    /**
     * 存储
     */
    fun putString(key: String, value: String) {
        mEditor.putString(key, value)
        mEditor.commit()
    }

    /**
     * 存储
     */
    fun putLong(key: String, value: Long) {
        mEditor.putLong(key, value)
        mEditor.commit()
    }

    /**
     * 存储
     */
    fun putInt(key: String, value: Int) {
        mEditor.putInt(key, value)
        mEditor.commit()
    }

    /**
     * 存储
     */
    fun putBoolean(key: String, value: Boolean) {
        mEditor.putBoolean(key, value)
        mEditor.commit()
    }

    /**
     * 存储
     */
    fun putFloat(key: String, value: Float) {
        mEditor.putFloat(key, value)
        mEditor.commit()
    }

    /**
     * 获取保存的数据
     */
    fun getString(key: String, defaultValue: String): String {
        return mSharedPreferences.getString(key, defaultValue) ?: defaultValue
    }

    /**
     * 获取保存的数据
     */
    fun getInt(key: String, defaultValue: Int): Int {
        return mSharedPreferences.getInt(key, defaultValue)
    }

    /**
     * 获取保存的数据
     */
    fun getBoolean(key: String, defaultValue: Boolean): Boolean {
        return mSharedPreferences.getBoolean(key, defaultValue)
    }

    /**
     * 获取保存的数据
     */
    fun getFloat(key: String, defaultValue: Float): Float {
        return mSharedPreferences.getFloat(key, defaultValue)
    }

    /**
     * 获取保存的数据
     */
    fun getLong(key: String, defaultValue: Long): Long {
        return mSharedPreferences.getLong(key, defaultValue)
    }

    /**
     * 移除某个key值已经对应的值
     */
    fun remove(key: String) {
        mEditor.remove(key)
        mEditor.commit()
    }

    /**
     * 清除所有数据
     */
    fun clear() {
        mEditor.clear()
        mEditor.commit()
    }

    /**
     * 查询某个key是否存在
     */
    fun contain(key: String): Boolean? {
        return mSharedPreferences.contains(key)
    }

    /**
     * 返回所有的键值对
     */
    fun getAll(): Map<String, *> {
        return mSharedPreferences.all
    }
}